package com.cynteka.cyfoman.ui;

import com.cynteka.cyfoman.core.config.Explorer;
import org.jdesktop.swingx.JXTree;
import org.jdesktop.swingx.JXTreeTable;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * User: Sergey
 * Date: 28.07.11
 * Time: 0:45
 */
public class ExplorerPanelBuilder {
    public void createContent(Container container, Explorer explorer) {
        JSplitPane splitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPanel.setDividerLocation(600);
        container.add(explorer.getTitle(), splitPanel);

        File baseFolder = new File(explorer.getBaseFolder());
        // ExtendedFileSystemModel model = new ExtendedFileSystemModel(baseFolder);
        final ExplorerFileSystemModel model = new ExplorerFileSystemModel(explorer);

        final JXTreeTable treeTable = new JXTreeTable();
        treeTable.setTreeTableModel(model);

        File explorersFolder = new File(Parameters.getQueriesFileName()).getParentFile();

        if (explorer.getIcon() != null) {
            File iconFile = new File(explorersFolder, explorer.getIcon());
            treeTable.setDefaultRenderer(Icon.class, new IconCellRenderer(iconFile.getAbsolutePath()));
        }
        treeTable.getTableHeader().setVisible(true);
        TreeCellRenderer tcr = treeTable.getTreeCellRenderer();
        JXTree.DelegatingRenderer delegatingRenderer = (JXTree.DelegatingRenderer) tcr;
        final TreeCellRenderer oldRenderer = delegatingRenderer.getDelegateRenderer();
        delegatingRenderer.setDelegateRenderer(new TreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                if (value instanceof File) {
                    File f = (File) value;
                    if (f.isFile()) {
                        Icon icon = FileSystemView.getFileSystemView().getSystemIcon(f);
                        return new JLabel(f.getName(), icon, JLabel.LEFT);
                    }
                }
                return oldRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
            }
        });

        splitPanel.add(new JScrollPane(treeTable));
        treeTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() > 1) {
                    TreePath path = treeTable.getTreeSelectionModel().getSelectionPath();
                    File f = (File) path.getLastPathComponent();
                    if (f.isFile()) {
                        try {
                            String cmd = "rundll32 url.dll,FileProtocolHandler \"" + f.getCanonicalPath() + "\"";
                            Process p = Runtime.getRuntime().exec(cmd);
//
//                            Process p = Runtime.getRuntime().exec("cmd start " + f.getAbsolutePath());
                            InputStream os = p.getInputStream();
                            while (os.available() > 0) {
                                int c = os.read();
                                System.out.print((char) c);
                            }
                        } catch (IOException e1) {
                            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }

                    }
                }
            }
        }

        );

        final EventDispatcher eventDispatcher = (EventDispatcher) Context.getBeanFactory().getBean("eventDispatcher");
        eventDispatcher.addEventListener(UIEvent.REFRESH, new IEventListener() {
            @Override
            public void handleEvent(IEvent event) {
                if (treeTable.isShowing()) {
                    model.refresh();
                }
            }
        });

    }
}
